package com.element.security.single.context;

import com.common.core.exception.ExceptionFactory;
import com.element.security.single.model.AuthUserE;
import com.element.security.single.utils.SecurityUtil;
import org.springframework.util.CollectionUtils;

import java.util.Map;

/**
 * @auther WuJun.Zhang
 * @date 2022/2/22 下午5:08
 */
public class SecurityContext {

    public static AuthUserE getUser() {
        AuthUserE authUser = SecurityUtil.getUser();
        if (null == authUser) {
            throw ExceptionFactory.bizException("用户不存在");
        }
        return authUser;
    }

    // 根据拓展KEY获取拓展值
    public static <T> T getExpValue(String key, Class<T> clazz) {
        AuthUserE authUser = getUser();
        Map<String, Object> expMap = authUser.getExpMap();
        if (CollectionUtils.isEmpty(expMap)) {
            throw ExceptionFactory.bizException("拓展字段为空");
        }
        Object value = expMap.get(key);
        if (null == value) {
            throw ExceptionFactory.bizException("拓展值不存在");
        }
        return clazz.cast(value);
    }
}